package com.hermes.biz.common.util.security;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RSATester {
	static String publicKey;
    static String privateKey;

    static String PASSWORD = "12345678";
    
    public static String content = "1234567890 zz abcdefghijklmnopqrstuvwxyz 啊伯吃的额火哥 好吧，我写不下去了，太多了好不好，还是不能用有木有！";
     static {
         try {
            Map keyMap = RSAUtils.genKeyPair();
            publicKey = RSAUtils.getPublicKey(keyMap);
            privateKey = RSAUtils.getPrivateKey(keyMap);
         } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
     public static void main(String[] args) throws Exception {
    	 // testAES();
    	 testRSAandAES();
    	 //test();
        //testSign();
    }

     static void testAES() throws NoSuchProviderException{
    	 String str = content;
         String password = "12345678";
         
         //AES加密
         System.out.println("========================================");
         System.out.println("| AES 开始加密");
         System.out.println("========================================");
         System.out.println("---> 加密前：" + content);
         byte[] encryptResult =  null;
		encryptResult = AESUtil.encrypt(content, password);
         String encryptResultStr = AESUtil.parseByte2HexStr(encryptResult);
         System.out.println("---> 加密后：" + encryptResultStr);
         //AES解密
         byte[] decryptFrom = AESUtil.parseHexStr2Byte(encryptResultStr);
         byte[] decryptResult = null;
		decryptResult = AESUtil.decrypt(decryptFrom,password);
         System.out.println("---> 解密后：" + new String(decryptResult));
     }
     
     static void testRSAandAES() throws Exception {
         String source = "你好，AES编码加密呵呵哈哈阿里郎了dlfjladal你好，AES编码加密呵呵哈哈阿里郎了dlfjladald 叮叮当当你好，AES编码加密呵呵哈哈阿里郎了dlfjladald 叮叮当当你好，AES编码加密呵呵哈哈阿里郎了dlfjladald 叮叮当当你好，AES编码加密呵呵哈哈阿里郎了dlfjladald 叮叮当当d 叮叮当当";
         content = source;
         byte[] data = content.getBytes("UTF-8");
         System.out.println("========================================");
         System.out.println("| RSA 开始加密");
         System.out.println("========================================");
         System.out.println("---> RSA加密前数据：\r\n" + content);
         //RSA 加密：
         byte[] _RSAencodedData = RSAUtils.encryptByPublicKey(data, publicKey);
         System.out.println("---> RSA加密后数据：\r\n" + new String(_RSAencodedData));
         System.out.println("---> AES加密前长度："+_RSAencodedData.length);
         byte[] de = null;
         try {
     		System.out.println("--->AES加密前数据："+new String(_RSAencodedData));
     		//AES 加密
 			byte[] en = AESUtil.encrypt(_RSAencodedData);
 			System.out.println("--->加密后数据："+ en);
 			// AES 解密
 			de = AESUtil.decrypt(en);
 			System.out.println("--->解密后数据："+de);
 		} catch (InvalidKeyException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (NoSuchAlgorithmException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (InvalidKeySpecException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (NoSuchPaddingException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (InvalidParameterSpecException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (IllegalBlockSizeException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (BadPaddingException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (UnsupportedEncodingException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (InvalidAlgorithmParameterException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}

         //RSA再解密
         byte[] decodedData = RSAUtils.decryptByPrivateKey(de, privateKey);
         String target = new String(decodedData,"UTF-8");
         System.out.println("---> RSA再解密后数据: \r\n" + target);
     }
     
     static void testSign() throws Exception {
        byte[] data = content.getBytes();
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
        byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        String target = new String(decodedData);
        String sign = RSAUtils.sign(encodedData, privateKey);
        boolean status = RSAUtils.verify(encodedData, publicKey, sign);
    }
}
